import time
import requests
import random
from algorithm.XC_spider import get_flight_info
from datetime import datetime, timedelta
from algorithm.city_code import day,name_code,depart_arrival
import pandas as pd
import algorithm.dataprocess

# 爬取票价信息
def get_flight(departcity, arrivalcity,start_date):
    depart_city_code = name_code(name=departcity)
    arrival_city_code = name_code(name=arrivalcity)
    try:
        flightNo = []
        marketAirlineName = []
        departureCityName = []
        departureAirportName = []
        arrivalCityName = []
        arrivalAirportName = []
        departureDateTime = []
        arrivalDateTime = []
        price = []
        duration = []
        discountRate = []
        aircraftName = []
        # 结束日期
        end_date = datetime.now().date() + timedelta(days=day)
        #print(start_date,end_date)
        # 获取两个日期之间的所有日期
        current_date = start_date
        while current_date <= end_date:
            # 格式化日期为字符串
            formatted_date = current_date.strftime('%Y-%m-%d')
            print(f'爬取{departcity}飞往{arrivalcity}第{formatted_date}的数据')
            _, flight_list_data = get_flight_info(depart_city_code, arrival_city_code, formatted_date)

            for flight_list in flight_list_data:
                # aircraftName可能为空，如果为空则不要这条数据
                if flight_list["flightSegments"][0]["flightList"][0].get("aircraftName"):
                    duration.append(flight_list["flightSegments"][0]["duration"])
                    flightNo.append(flight_list["flightSegments"][0]["flightList"][0]["flightNo"])
                    marketAirlineName.append(flight_list["flightSegments"][0]["flightList"][0]["marketAirlineName"])
                    departureCityName.append(flight_list["flightSegments"][0]["flightList"][0]["departureCityName"])
                    departureDateTime.append(flight_list["flightSegments"][0]["flightList"][0]["departureDateTime"])
                    departureAirportName.append(flight_list["flightSegments"][0]["flightList"][0]["departureAirportName"])
                    arrivalDateTime.append(flight_list["flightSegments"][0]["flightList"][0]["arrivalDateTime"])
                    arrivalAirportName.append(flight_list["flightSegments"][0]["flightList"][0]["arrivalAirportName"])
                    arrivalCityName.append(flight_list["flightSegments"][0]["flightList"][0]["arrivalCityName"])
                    price.append(flight_list["priceList"][0]["adultPrice"])
                    discountRate.append(flight_list["priceList"][0]["priceUnitList"][0]["flightSeatList"][0]["discountRate"])
                    aircraftName.append(flight_list["flightSegments"][0]["flightList"][0]["aircraftName"])
            delay = random.uniform(1.5, 3)
            time.sleep(delay)
            # 日期加一天
            current_date += timedelta(days=1)

        data = {'flightNo': flightNo, 'airlineName': marketAirlineName,
                    'departureCityName': departureCityName,'departureAirportName':departureAirportName,
                    'arrivalCityName': arrivalCityName, 'arrivalAirportName':arrivalAirportName,
                    'departureDateTime': departureDateTime,'arrivalDateTime': arrivalDateTime,
                    'price': price, 'duration': duration, 'discountRate': discountRate,
                    'aircraftName':aircraftName}

        df = pd.DataFrame(data)
        #res = get_calendar_detail(depart_city_code, arrival_city_code)['data']
        #print(res)
        #print(flight_list_data)
        df.to_excel(f'携程航班数据-{departcity}-{arrivalcity}.xlsx', index=False, header=True)
        print('爬取完毕')
    except requests.RequestException as e:
        print(f'{departcity}飞往{arrivalcity}爬取失败，失败原因为：{e}')

def update():
    df = pd.read_excel('携程航班处理数据.xlsx')
    df.loc[:] = ''
    df.to_excel('携程航班处理数据.xlsx', index=False)
    algorithm.dataprocess.itineraryClear()
    list_info = depart_arrival()
    for tuple in list_info:
        # 开始日期
        start_date = datetime.now().date() + timedelta(days=1)
        get_flight(tuple[0], tuple[1], start_date)
        # 处理合并训练集
        algorithm.dataprocess.processTrain(tuple[0], tuple[1], datetime.now().date())
        algorithm.dataprocess.combination(tuple[0], tuple[1])
        # 处理插入真实数据
        algorithm.dataprocess.insertItinerary(tuple[0], tuple[1])